{extend name="base" /}
<!-- page specific plugin styles -->
{block name="inline-styles"}
{/block}
<!-- PAGE CONTENT BEGINS -->
{block name="content"}
<div class="page js_show">
    <div class="page__bd" style="height: 100%">
        <div class="weui-tab">
            <div class="weui-navbar">
                <a class="weui-navbar__item {eq name='status' value='0'}weui-bar__item_on{/eq}" href="{:url('Index/order')}?id=0">
                    全部订单
                </a>
                <a class="weui-navbar__item {eq name='status' value='1'}weui-bar__item_on{/eq}" href="{:url('Index/order')}?id=1">
                    待发货
                </a>
                <a class="weui-navbar__item {eq name='status' value='3'}weui-bar__item_on{/eq}" href="{:url('Index/order')}?id=3">
                    已发货
                </a>
                <a class="weui-navbar__item {eq name='status' value='5'}weui-bar__item_on{/eq}" href="{:url('Index/order')}?id=5">
                    已完成
                </a>
                <a class="weui-navbar__item {eq name='status' value='4'}weui-bar__item_on{/eq}" href="{:url('Index/order')}?id=4">
                    申换/售后
                </a>
            </div>
            <div class="weui-tab__panel" id="listData" style="padding-bottom:60px;">
                
            </div>
            <div class="weui-tabbar">
                <a href="{:url('Index/home')}" class="weui-tabbar__item">
                    <div class="weui-tabbar__icon">
                        <i class="fa fa-home"></i>
                    </div>
                    <p class="weui-tabbar__label">主页</p>
                </a>
                <a href="{:url('Index/goods')}" class="weui-tabbar__item">
                    <div class="weui-tabbar__icon">
                        <i class="fa fa-list"></i>
                    </div>
                    <p class="weui-tabbar__label">下单</p>
                </a>
            </div>
        </div>
    </div>
</div>
{/block}
<!-- PAGE CONTENT ENDS -->
<!-- inline scripts related to this page -->
{block name="related-scripts"}
<script type="text/javascript">
    $.ajax({
        url: '/api/v1/goodsorder/lists',
        type: 'POST',
        dataType: 'json',
        data:{
            ispay:GetQueryString("id"),
            draw: 1
        }
    })
    .done(function(json) {
        console.log("success");
        renderData("#listData",json.data);
    })
    .fail(function() {
        console.log("error");
    })
    .always(function() {
        console.log("complete");
    });

    // 渲染列表
    function renderData(_elment,_data){
        for(var i= 0; i<_data.length;i++){
        	orderHtml = '<span class="weui-wepay-color-lightblue">'+_data[i].ispay+'</span>';
        	if(_data[i].status == 3){
        		//已发货,确认收货
        		orderHtml = '<button class="weui-btn weui-btn_mini weui-btn_primary" onclick="confirm_received('+_data[i].id+')">确认收货</button>';
        	}
            var _item =
            '<div class="weui-panel">\
                    <div class="weui-panel__bd">\
                        <div class="weui-cell">\
                            <div class="weui-cell__bd">\
                                <p>订单号：'+_data[i].oid+'</p>\
                                <p>收货人：'+_data[i].receive_name+'</p>\
                                <p>收货人电话：'+_data[i].receive_tel+'</p>\
                            </div>\
                            <div class="weui-cell__ft">'+
                            orderHtml
                            +'</div>\
                        </div>\
                        <a href="{:url('Index/order_detail')}?id='+_data[i].id+'" class="weui-media-box weui-media-box_appmsg" style="background-color: #fafafa">\
                            <div class="weui-media-box__hd">\
                                <img class="weui-media-box__thumb" src="'+_data[i].pic+'" alt="">\
                            </div>\
                            <div class="weui-media-box__bd">\
                                <h4 class="weui-media-box__title">'+_data[i].goodsname+'</h4>\
                                <p class="weui-media-box__desc">￥'+_data[i].sigprice+'</p>\
                            </div>\
                            <div class="weui-media-box__ft">\
                                <p>x'+_data[i].courier_num+'</p>\
                            </div>\
                        </a>\
                    </div>\
                    <div class="weui-panel__ft">\
                        <div class="weui-cell">\
                            <div class="weui-cell__bd">\
                            <p>共'+_data[i].courier_num+'件商品</p>\
                            <p>合计:￥'+_data[i].sigprice+'</p>\
                            </div>\
                            <div class="weui-cell__ft"><a href="{:url('Index/order_detail')}?id='+_data[i].id+'" class="weui-btn weui-btn_mini weui-btn_primary" >查看详情</a></div>\
                        </div>\
                    </div>\
                    </div>\
                </div>';

            $(_elment).append(_item);
        }

    }

    // 取消订单
    function orderCancel(_id) {
        // body...
        $.ajax({
            url: '/api/v1/goodsorder/cancel',
            type: 'POST',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            if(json.returnCode==1){
                $.toast("取消成功");
                location.reload();
            }else{
                $.toptip(json.returnMsg, 'error');
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }
    
    function confirm_received(_id){
    	res = confirm('您确认已经收到货了吗?')
    	if(!res){
    		return false;
    	}
    	// body...
        $.ajax({
            url: '/api/v1/goodsorder/confirm',
            type: 'POST',
            dataType: 'json',
            data: {id: _id},
        })
        .done(function(json) {
            if(json.returnCode==1){
                $.toast("确认成功");
                location.reload();
            }else{
                $.toptip(json.returnMsg, 'error');
            }
        })
        .fail(function() {
            console.log("error");
        })
        .always(function() {
            console.log("complete");
        });
    }

//重置浏览器回退链接
browserBack("home.html");
</script>
{/block}